<template>
  <div>
    <!-- 添加角色 -->
    <div>
      <MyDialog text="添加角色" title="添加角色" :myRef="myRef">
        <el-form :model="roleForm" :rules="rules" ref="roleForm">
          <el-form-item
            prop="name"
            label="角色名称"
            :label-width="formLabelWidth"
          >
            <el-input v-model="roleForm.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item
            prop="describe"
            label="角色描述"
            :label-width="formLabelWidth"
          >
            <el-input v-model="roleForm.describe" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
      </MyDialog>
    </div>
    <!-- 添加角色列表 -->
    <div class="mt20">
      <el-table :data="rolesData" border style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row
              :class="['bdbottom', i1 === 0 ? 'bdtop' : '', 'vcenter']"
              v-for="(item1, i1) in scope.row.children"
              :key="item1.id"
            >
              <!-- 一级权限 -->
              <el-col :span="5">
                <el-tag closable @close="removeRoles(scope.row, item1.id)">{{
                  item1.authName
                }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 二级和三级 -->
              <el-col :span="19">
                <!-- 通过for循环 渲染二级权限 -->
                <el-row
                  :class="[i2 === 0 ? '' : 'bdtop', 'vcenter']"
                  v-for="(item2, i2) in item1.children"
                  :key="item2.id"
                >
                  <el-col :span="6">
                    <el-tag
                      type="success"
                      closable
                      @close="removeRoles(scope.row, item2.id)"
                      >{{ item2.authName }}</el-tag
                    >
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      type="warning"
                      v-for="item3 in item2.children"
                      @close="removeRoles(scope.row, item3.id)"
                      :key="item3.id"
                      closable
                      >{{ item3.authName }}</el-tag
                    >
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>

        <el-table-column label="#" width="50">
          <template slot-scope="props">
            {{ props.$index + 1 }}
          </template>
        </el-table-column>

        <el-table-column label="角色名称" prop="roleName"> </el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="props">
            <el-button type="warning" @click="authority(props.row)" size="small"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 树状图 -->
      <el-dialog
        @close="fn"
        title="收货地址"
        :visible.sync="dialogTableVisible"
      >
        <el-tree
          :data="data"
          show-checkbox
          ref="tree"
          node-key="id"
          :default-checked-keys="defaultKeys"
          default-expand-all
          :props="defaultProps"
        >
        </el-tree>

        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogTableVisible = false">取 消</el-button>
          <!-- <template slot-scope="props"> -->
            <el-button type="primary" @click="authSuccess">确 定</el-button>
          <!-- </template> -->
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import MyDialog from "../../components/MyDialog.vue";
import rules from "../../rules/roles";
import {
  getRoles,
  deleteRoles,
  getTree,
  rolesAuthority,
} from "../../api/roles";
export default {
  created() {
    this.init();
  },
  data() {
    return {
      myRef: null,
      roleForm: {},
      formLabelWidth: "120px",
      rules,
      rolesData: [],
      dialogTableVisible: false,
      data: [],
      defaultProps: {
        children: "children",
        label: "authName",
      },
      defaultKeys: [],
      roleId: null
    };
  },
  components: {
    MyDialog,
  },
  // 删除  v-del="{ url: "url", id: id }"
  methods: {
    changeTree(a, b) {
      console.log(a, b)
    },
    async authSuccess() {
      console.log(this.$refs.tree.getHalfCheckedKeys());
      let rids = [
        ...this.$refs.tree.getHalfCheckedKeys(),
        ...this.$refs.tree.getCheckedKeys()
      ].join(',')

      await rolesAuthority({ roleId: this.roleId, rids })
      this.init()

      this.dialogTableVisible = false

    },
    fn() {
      this.dialogTableVisible = false;
      this.defaultKeys = [];
    },
    // 分配权限
    async authority(row) {
      this.dialogTableVisible = true;
      this.roleId = row.id
      let res = await getTree();
      this.data = res;

      this.getLeafkeys(row, this.defaultKeys);
    },
    getLeafkeys(node, arr) {
      if (!node.children) {
        return arr.push(node.id);
      }
      node.children.forEach((item) => this.getLeafkeys(item, arr));
    },
    async removeRoles(row, rightId) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await deleteRoles({ roleId: row.id, rightId });
          row.children = res;

          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async init() {
      let res = await getRoles();
      this.rolesData = res;
    },
  },
};
</script>

<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}

.el-table {
  font-size: 12px !important;
}

.el-tag {
  margin: 7px;
}
.bdtop {
  border-top: 1px solid #eee;
}
.bdbottom {
  border-bottom: 1px solid #eee;
}
.vcenter {
  display: flex;
  align-items: center;
}
</style>